package com.fanhang.samples.spring.security.common;

import java.util.ArrayList;
import java.util.List;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fanhang.samples.spring.security.po.User;

public class SampleUserDetails extends org.springframework.security.core.userdetails.User 
	implements UserDetails {
	
	private static final long serialVersionUID = 1L;
	
	private com.fanhang.samples.spring.security.po.User user;
	
	public SampleUserDetails(User user) {
		super(user.getUserName(), user.getPassword(), createGrantedAuthority(user.getRoles()));
		this.user = user;
	}
	
	private static List<GrantedAuthority> createGrantedAuthority(List<String> roles) {
		List<GrantedAuthority> auths = new ArrayList<GrantedAuthority>();
		for (String role : roles) {
			GrantedAuthority auth = new SimpleGrantedAuthority(role);
			auths.add(auth);
		}
		return auths;
	}
	
	public User getUser() {
		return user;
	}
	
}
